package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.ActivityDAO;
import com.proactive.webapp.entity.Activity;

public class ActivityDAOImpl implements ActivityDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public Activity getByStatus(Activity activity) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM activity WHERE activityID = ? AND activityStatus = ?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activity.getId());
			smt.setInt(2, activity.getStatus());
			rs = smt.executeQuery();
			if(rs.next()){
				activity.setTitle(rs.getString("activityTitle"));
				activity.setContent(rs.getString("activityContent"));
				activity.setBannerPath(rs.getString("activityBanner"));
				activity.setShareContent(rs.getString("activityShareContent"));
				activity.setShareImagePath(rs.getString("activityShareImage"));
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return activity;
	}

	public List<Activity> getActivityList(){
		List<Activity> activityList = new ArrayList<Activity>();
		String sql = "SELECT * FROM activity";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			rs = smt.executeQuery();
			while(rs.next()){
				Activity activity = new Activity();
				activity.setId(rs.getInt("activityID"));
				activity.setTitle(rs.getString("activityTitle"));
				activity.setContent(rs.getString("activityContent"));
				activity.setStatus(rs.getInt("activityStatus"));
				activityList.add(activity);
			}
			rs.close();
			smt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return activityList;
	}
	
	public void update(Activity activity) {
		// TODO Auto-generated method stub
		String sql = "UPDATE activity SET activityTitle=?, activityContent=?, "
				+ "activityBanner=?, activityStatus=?, activityShareImage=?, "
				+ "activityShareContent=? WHERE activityID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, activity.getTitle());
			smt.setString(2, activity.getContent());
			smt.setString(3, activity.getBannerPath());
			smt.setInt(4, activity.getStatus());
			smt.setString(5, activity.getShareImagePath());
			smt.setString(6, activity.getShareContent());
			smt.setInt(7, activity.getId());
			smt.executeUpdate();			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}
	
	@Override
	public Activity get(Activity activity) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM activity WHERE activityID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, activity.getId());
			rs = smt.executeQuery();
			while(rs.next()){
				activity.setTitle(rs.getString("activityTitle"));
				activity.setContent(rs.getString("activityContent"));
				activity.setBannerPath(rs.getString("activityBanner"));
				activity.setStatus(rs.getInt("activityStatus"));
				activity.setShareImagePath(rs.getString("activityShareImage"));
				activity.setShareContent(rs.getString("activityShareContent"));
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return activity;
	}
}
